package com.baselibrary.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.baselibrary.R;
import com.baselibrary.view.GlideBlurTransformation;
import com.baselibrary.view.GlideRoundTransform;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;

public class GlideLoader {

    public static void LoderImage(Context context, String url, ImageView view) {
        LoderImage(context, url, view, 0);
    }

    public static void loadBannerImage(Context context, String url, ImageView view) {
        Glide.with(context).asBitmap().load(url).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                Drawable drawable = new BitmapDrawable(resource);
                view.setBackground(drawable);
            }
        });
    }

    public interface ICallback {
        void loadComplete(Drawable drawable);
    }

    //图片加载监听
    public static void loadListener(Context context, String url, ICallback callback) {
        Glide.with(context)
                .load(url)
                .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(new CustomTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        callback.loadComplete(resource);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {

                    }
                });
    }

    public static void LoderImage(Context context, String url, ImageView view, int round) {
        Glide.with(context)
                .load(url)
                .centerCrop()
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.placeholder)
                .transform(new GlideRoundTransform(context, round))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    public static void LoderCircleImage(Context context, String url, ImageView view) {
        Glide.with(context)
                .load(url)
                .centerCrop()
                .placeholder(R.drawable.circle_gray)
                .error(R.drawable.circle_gray)
                .transform(new GlideRoundTransform(context, 100))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    public static void LoadRoundCornerImage(Context context, String url, ImageView view) {
//        RoundedCornersTransform roundedCornersTransform = new RoundedCornersTransform(context, 6);
//        roundedCornersTransform.setExceptCorner(true, true, true, true);
        Glide.with(context)
                .load(url)
                .centerCrop()
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.placeholder)
                .transform(new GlideRoundTransform(context, 6))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    public static void LoadRoundCornerImageTwo(Context context, String url, ImageView view) {
        Glide.with(context)
                .load(url)
                .centerCrop()
                .error(R.drawable.placeholder)
                .transform(new GlideRoundTransform(context, 10))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    public static void LoderUserImage(Context context, String url, ImageView view, int round) {
        Glide.with(context)
                .load(url)
                .centerCrop()
                .placeholder(R.drawable.icon_placeholder)
                .error(R.drawable.icon_placeholder)
                .transform(new GlideRoundTransform(context, round))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    public static void LoderCenterInsideImage(Context context, String url, ImageView view) {
        Glide.with(context)
                .load(url)
                .centerInside()
                .placeholder(R.drawable.placeholder)
                .error(R.drawable.placeholder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    public static void LoderBlurImage(Context context, String url, ImageView view) {
        RequestOptions requestOptions = new RequestOptions();
        requestOptions
                .fitCenter()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideBlurTransformation(context));
        Glide.with(context)
                .load(url)
                .apply(requestOptions)
                .into(view);
    }

    public static void LoderLoadReleaseCoverImage(Context context, String url, ImageView view, int round) {
        Glide.with(context)
                .load(url)
                .centerCrop()
                .transform(new GlideRoundTransform(context, round))
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(view);
    }

    public static void LoderLoadReleaseCoverImagePlaceholder(Context context, String url, ImageView view, int round,int placeholderRes) {
        Glide.with(context)
                .load(url)
                .centerCrop()
                .placeholder(placeholderRes)
                .transform(new GlideRoundTransform(context, round))
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(view);
    }

    public static void LoderDrawable(Context context, int drawable, ImageView view, int round) {
        Glide.with(context)
                .load(drawable)
                .fitCenter()
                .transform(new GlideRoundTransform(context, round))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    public static Bitmap load(Context context, String url) {
        try {
            return Glide.with(context)
                    .asBitmap()
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
